package com.whut.whw.util;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.whut.whw.BuildConfig;

/**
 * <pre>
 *     author : 杨丽金
 *     time   : 2017/05/25
 *     desc   : 日志工具类
 *     version: 1.0
 * </pre>
 */
public class LogUtils {
    // 读取BuildConfig中的变量，控制是否打印Log
    private static final boolean ENABLE_LOG = BuildConfig.ENABLE_LOG;

    /**
     * 打印Error级别普通日志
     *
     * @param tag
     * @param msg
     */
    public static void e(String tag, String msg) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag(tag)   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();
        Logger.clearLogAdapters();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        if (ENABLE_LOG) Logger.e(msg);
    }


    /**
     * 输出格式化的Error级别日志
     * @param tag：日志标签
     * @param format：格式，形如"hello %s"
     * @param args：参数，"world"
     */
    public static void e(String tag, String format,  Object... args) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag(tag)   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();
        Logger.clearLogAdapters();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        if (ENABLE_LOG) Logger.e(format, args);
    }

    /**
     * 打印json字符串（在debug级别）
     *
     * @param tag
     * @param json
     */
    public static void json(String tag, String json) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag(tag)   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();
        Logger.clearLogAdapters();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        if (ENABLE_LOG) Logger.json(json);
    }


    /**
     * 打印debug级别普通日志
     *
     * @param tag
     * @param msg
     */
    public static void d(String tag, String msg) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag(tag)   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();
        Logger.clearLogAdapters();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        if (ENABLE_LOG) Logger.d(msg);
    }

    /**
     * 输出格式化的debug级别日志
     * @param tag
     * @param format：格式，形如"hello %s"
     * @param args：参数，"world"
     */
    public static void d(String tag, String format,  Object... args) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag(tag)   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();
        Logger.clearLogAdapters();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        if (ENABLE_LOG) Logger.d(format, args);
    }

    /**
     * 打印map，set，list，array（只能在debug级别的日志中打印集合）
     *
     * @param tag
     * @param object
     */
    public static void d(String tag, Object object) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag(tag)   // (Optional) Custom tag for each log. Default PRETTY_LOGGER
                .build();
        Logger.clearLogAdapters();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
        if (ENABLE_LOG) Logger.d(object);
    }


}
